package com.keyroy.game.puzzle_rpg.dialogs;

import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import com.keyroy.game.puzzle_rpg.models.environment.Project;
import com.keyroy.game.puzzle_rpg.models.interfaces.EventListener;
import com.keyroy.game.puzzle_rpg.models.puzzle.TileType;
import com.keyroy.game.puzzle_rpg.models.skill.SkillCost;
import com.keyroy.game.puzzle_rpg.views.tables.SkillCostTableView;

public class SkillCostEditor extends Shell {
	private SkillCost skillCost;
	private SkillCostTableView skill_cost_table_view;

	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String args[]) {
		try {
			Display display = Display.getDefault();
			SkillCostEditor shell = new SkillCostEditor(display, null);
			shell.open();
			shell.layout();
			while (!shell.isDisposed()) {
				if (!display.readAndDispatch()) {
					display.sleep();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * @wbp.parser.constructor
	 */
	public SkillCostEditor(Display display, final EventListener<SkillCost> listener) {
		this(display, null, listener);
	}

	public SkillCostEditor(Display display, SkillCost sc, final EventListener<SkillCost> listener) {

		super(display, SWT.DIALOG_TRIM);
		setLayout(new FormLayout());
		if (sc == null) {
			this.skillCost = new SkillCost();
			List<TileType> tileTypes = Project.getProject().getPuzzleSet().tileTypes;
			for (int i = 0; i < tileTypes.size(); i++) {
				skillCost.add(tileTypes.get(i), 0);
			}

		} else {
			this.skillCost = sc;
		}

		Button btnNewButton = new Button(this, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (listener != null) {
					skillCost.trim();
					listener.onEvent(skillCost);
				}
			}
		});
		FormData fd_btnNewButton = new FormData();
		fd_btnNewButton.top = new FormAttachment(0, 235);
		fd_btnNewButton.left = new FormAttachment(0, 330);
		fd_btnNewButton.right = new FormAttachment(100, -10);
		btnNewButton.setLayoutData(fd_btnNewButton);
		btnNewButton.setText("\u5B8C\u6210");

		skill_cost_table_view = new SkillCostTableView(this);
		FormData fd_skill_cost_table_view = new FormData();
		fd_skill_cost_table_view.bottom = new FormAttachment(100, -43);
		fd_skill_cost_table_view.right = new FormAttachment(100, -10);
		fd_skill_cost_table_view.top = new FormAttachment(0, 10);
		fd_skill_cost_table_view.left = new FormAttachment(0, 10);
		skill_cost_table_view.setLayoutData(fd_skill_cost_table_view);
		createContents();

		skill_cost_table_view.set(skillCost);
	}

	/**
	 * Create contents of the shell.
	 */
	protected void createContents() {
		setText("\u6280\u80FD\u6D88\u8017\u7F16\u8F91");
		setSize(450, 300);

	}

	@Override
	protected void checkSubclass() {
	}

	public void reset() {
		skill_cost_table_view.reset();
	}
}
